package com.xiaoq.shop.endpoint;

import com.xiaoq.BaseEndpoint2B;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.shop.entity.ALiPayConfigEntity;
import com.xiaoq.shop.service.IPayConfigService;
import com.xiaoq.shop.vo.ALiPayConfigVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * Created by chenjh on 2018/1/10.
 */
@RestController
@RequestMapping(value = "/api/v1/shops/payconfig")
public class PayConfigEndpoint2B extends BaseEndpoint2B {

    @Autowired
    private IPayConfigService payConfigService;

    @Autowired
    private IdGenerator idGenerator;

    @ApiOperation(value="添加支付宝账户配置信息", notes="添加支付宝账户配置信息")
    @ApiResponses({
            @ApiResponse(code = 200, response = GenericResponse.class, message = "添加支付宝账户配置信息")
    })
    @PostMapping(value="/alipay", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> addALiPayConfig(@RequestBody ALiPayConfigVo aLiPayConfigVo) {

        if(StringUtils.isAnyBlank(aLiPayConfigVo.getShopId(), aLiPayConfigVo.getaLiPayAppId(),
                aLiPayConfigVo.getaLiPayAppPrivateKey(), aLiPayConfigVo.getaLiPayPublicKey())){
            return GenericResponse.ng("参数不全");
        }
        ALiPayConfigEntity aLiPayConfigEntity = new ALiPayConfigEntity();
        BeanUtils.copyProperties(aLiPayConfigVo, aLiPayConfigEntity);
        aLiPayConfigEntity.setId(idGenerator.nextId());
        boolean retFlag = payConfigService.insertALiPayConfig(aLiPayConfigEntity);
        if(retFlag){
            return GenericResponse.ok();
        }
        logger.error("添加支付宝支付账户失败");
        return GenericResponse.ng("添加支付宝支付账户失败");
    }

    @ApiOperation(value="删除支付宝账户配置信息", notes="删除支付宝账户配置信息")
    @ApiResponses({
            @ApiResponse(code = 200, response = GenericResponse.class, message = "删除支付宝账户配置信息")
    })
    @DeleteMapping(value="/alipay", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> deleteALiPayConfig(@ApiParam(value="shopId", required=true) @RequestParam String shopId) {

        if(StringUtils.isBlank(shopId)){
            return GenericResponse.ng("shopId为空，删除支付宝账户失败");
        }
        boolean retFlag = payConfigService.deleteAliPayConfigByShopId(shopId);
        if(retFlag){
            return GenericResponse.ok();
        }
        logger.error("数据库删除支付宝账户失败shopId: " + shopId);
        return GenericResponse.ng("数据库删除支付宝账户失败shopId: " + shopId);
    }



    @ApiOperation(value="删除微信账户配置信息", notes="删除微信账户配置信息")
    @ApiResponses({
            @ApiResponse(code = 200, response = GenericResponse.class, message = "删除微信账户配置信息")
    })
    @DeleteMapping(value="/wxpay", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> deleteWXPayConfig(@ApiParam(value="shopId", required=true) @RequestParam String shopId,
                                               @ApiParam(value="wxPayType", required=true) @RequestParam String wxPayType) {

        if(StringUtils.isBlank(shopId)){
            return GenericResponse.ng("shopId为空，删除微信账户失败");
        }
        boolean retFlag = payConfigService.deleteWXPayConfigByShopId(shopId, wxPayType);
        if(retFlag){
            return GenericResponse.ok();
        }
        logger.error("数据库删除微信账户失败shopId: " + shopId);
        return GenericResponse.ng("数据库删除微信账户失败shopId: " + shopId);
    }



}
